package com.dianrui.qiyouriding.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.dianrui.qiyouriding.Constant;
import com.dianrui.qiyouriding.R;
import com.dianrui.qiyouriding.util.NoDoubleClickUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 合伙人的webview
 */
public class PartnerWebViewActivity extends BaseActivity {
    private boolean isSuccess = false;
    private boolean isError = false;
    private LinearLayout ll_control_error;

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.webview)
    WebView webview;
    @BindView(R.id.act_progressbar)
    ProgressBar mProgressBar;
    @BindView(R.id.show_webview_layout)
    LinearLayout showWebviewLayout;


    @Override
    public int getContentViewId() {
        return R.layout.partner_webview;
    }

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    public void initView() {
        customInit(true, R.color.main_color);
        title.setText(getString(R.string.my_txt12));

        webview.post(new Runnable() {
            @Override
            public void run() {
                webview.loadUrl(Constant.PANTERURL);
                WebSettings webSetting = webview.getSettings();
                webSetting.setJavaScriptEnabled(true);
                webSetting.setUseWideViewPort(true);
                webSetting.setLoadWithOverviewMode(true);
                webSetting.setAppCacheEnabled(false);
                webSetting.setSupportZoom(true);
                webSetting.setBuiltInZoomControls(true);
                webSetting.setDisplayZoomControls(false);
                webSetting.setDomStorageEnabled(true);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    webSetting.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
                }

                webview.setWebViewClient(new WebViewClient() {
                    @Override
                    public void onPageStarted(WebView view, String url, Bitmap favicon) {
                        super.onPageStarted(view, url, favicon);
                        if (mProgressBar != null) {
                            mProgressBar.setVisibility(View.VISIBLE);
                        }


                    }

                    @Override
                    public void onPageFinished(WebView view, String url) {
                        super.onPageFinished(view, url);
                        if (!isError) {
                            isSuccess = true;
                            //回调成功后的相关操作
                            ll_control_error.setVisibility(View.GONE);
                            showWebviewLayout.setVisibility(View.VISIBLE);
                            webview.setVisibility(View.VISIBLE);
                        } else {
                            isError = false;
                            webview.setVisibility(View.GONE);
                            showWebviewLayout.setVisibility(View.GONE);
                            ll_control_error.setVisibility(View.VISIBLE);
                        }

                    }
                    //加载错误的时候会回调
                    @RequiresApi(api = Build.VERSION_CODES.M)
                    @Override
                    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                        super.onReceivedError(view, errorCode, description, failingUrl);
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                            return;
                        }
                        isError = true;
                        isSuccess = false;
                        //6.0以下执行
                        webview.setVisibility(View.GONE);
                        showWebviewLayout.setVisibility(View.GONE);
                        ll_control_error.setVisibility(View.VISIBLE);

                    }


                    @Override
                    public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                        super.onReceivedError(view, request, error);
                        isError = true;
                        isSuccess = false;
                        //6.0以上执行
                        webview.setVisibility(View.GONE);
                        showWebviewLayout.setVisibility(View.GONE);
                        ll_control_error.setVisibility(View.VISIBLE);

                    }


                    @Override
                    public boolean shouldOverrideUrlLoading(WebView view, String url) {
                        view.loadUrl(url);
                        return true;
                    }
                });
                webview.setWebChromeClient(new WebChromeClient() {
                    @Override
                    public void onProgressChanged(WebView view, int newProgress) {
                        super.onProgressChanged(view, newProgress);
                        //设置进度条的最大值
                        if (mProgressBar != null) {
                            mProgressBar.setProgress(newProgress);
                        }
                    }

                });
            }
        });
        ll_control_error = (LinearLayout) findViewById(R.id.ll_control_error);
        Button errorBtn=ll_control_error.findViewById(R.id.online_error_btn_retry);
        TextView errorTitle=ll_control_error.findViewById(R.id.title);
        errorTitle.setText("找不到网页");
        errorBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ll_control_error.setVisibility(View.GONE);
                showWebviewLayout.setVisibility(View.VISIBLE);
                webview.reload();
            }
        });

    }

    @Override
    public void initData() {}



    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && webview.canGoBack()) {
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }




    @Override
    protected void onResume() {
        super.onResume();
        if (webview != null) {
            webview.onResume();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (webview != null) {
            webview.onPause();
        }
    }


    @OnClick({R.id.back, R.id.coption})
    public void onViewClicked(View view) {
        if (NoDoubleClickUtils.isDoubleClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.coption:
                startActivity(new Intent(PartnerWebViewActivity.this, CooperationActivity.class));
                break;

                default:
                    break;
        }
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (webview != null) {
            webview.clearCache(true);
            webview.stopLoading();
            //防止系统在3.x的手机，在缩放按钮未完全关闭时候退出的闪退
            webview.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (webview != null) {
                        webview.destroy();
                        webview = null;
                    }
                }
            }, 3000);
        }
    }

}
